import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.*;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import entity.WebPage;
import java.io.File;
import java.util.Date;

	
public class LuceneIndexer {

	private static LuceneIndexer indexer = null;
	private static IndexWriter indexWriter = null;
	private static File file;
	private static Directory directory;
	
	
	private LuceneIndexer () {
		try {
			
			// 1. create the index
			file = new File ("D:\\lucene\\index"); 
			directory = FSDirectory.open(file);
			indexWriter = new IndexWriter(directory, new StandardAnalyzer(Version.LUCENE_29), IndexWriter.MaxFieldLength.UNLIMITED);
		}
		catch(Exception e) {
			e.printStackTrace();

		}

	}
	
	public static LuceneIndexer getInstance() {
		if(indexer == null) {
			indexer = new LuceneIndexer();
		}
		return indexer;
	}
	
	
		
	public boolean indexUrl(WebPage webpage) {
		try {
		
			
			// the boolean arg in the IndexWriter ctor means to
			// create a new index, overwriting any existing index
		
			Document doc = new Document();
			
			//if description or title is empty, it will mean either the file we are parse is a text file or a html file that does not declare the description in the meta data or title in the web page
			String url = webpage.getUrl();
			
				//if it is a text file, we will use the text file name as the name
			if(url.endsWith(".txt")) {
				
				webpage.setDescriptionOrTitle(url.substring(url.lastIndexOf("/")+1,url.lastIndexOf(".")));
			}
			//else if it is a bad html coding with no title, we use the 1st 50 chacter to be it name 
					
		    doc.add(new Field("name", webpage.getDescriptionOrTitle(), Field.Store.YES, Field.Index.ANALYZED));
			doc.add(new Field("url", webpage.getUrl(), Field.Store.YES, Field.Index.ANALYZED));
			doc.add(new Field("description", webpage.getContent(), Field.Store.YES, Field.Index.ANALYZED));
			doc.add(new Field("tags", webpage.getKeywords(), Field.Store.YES, Field.Index.ANALYZED));
			doc.add(new Field("date", new Date().toString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
		    indexWriter.addDocument(doc);
		    
		    indexWriter.commit();

		}
		catch(Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}
		
		public void searchString(String queryStr){

			try {
				
				IndexSearcher indexSearcher = new IndexSearcher(directory, true);
				
				Version version = Version.LUCENE_29;
				Query query = new QueryParser(version, "url", new StandardAnalyzer(version)).parse(queryStr);

				TopScoreDocCollector collector = TopScoreDocCollector.create(20, true);

				indexSearcher.search(query, collector);
				
				ScoreDoc[] hits = collector.topDocs().scoreDocs;				
			    
			    System.out.println("Found " + hits.length + " hits.");
			    for(int i=0;i<hits.length;++i) {
			      int docId = hits[i].doc;
			      Document document = indexSearcher.doc(docId);
			      System.out.println((i + 1) + ". " + document.get("url"));
			    }


			}
			catch(Exception e) {
				e.printStackTrace();
			}
		}
		
		public void close() {
			try {
				 if (indexWriter != null) {
					 System.out.println("CLOSING");
			            indexWriter.close();
			            indexer = null;
			            file = null;
			            directory = null;
			        }
			}
			catch(Exception e) {
				e.printStackTrace();
			}

		}
		

}
